<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/3 11:54
// +----------------------------------------------------------------------

namespace app\single\model;


use cmf\lib\Http;
use cmf\lib\Sign;
use think\db\Where;
use think\facade\Cache;
use think\Model;
use app\single\model\RoleTaskLastUploadLogModel;

class RoleTaskModel extends Model
{
    public function initialize()
    {
        $this->table = config("database.game") . ".role_task";
    }

    //获取当前所有玩家的任务main_task id
    public function get_player_last_main_task()
    {
        $roleTaskLogModel = new RoleTaskLastUploadLogModel();
        $res = $this->select();
        $needUpLoadData = [];
        $cache = Cache::store("file");
        $serverInfo = $cache->get("server_info");
        foreach ($res as $row) {
            $playerId = $row["role_id"];
            $main_task = $row["main_task"];
            $main_task = str_replace("{", "", $main_task);
            $main_task = str_replace("}", "", $main_task);
            $main_task = explode(",", $main_task)[1];
            $oldRow = $roleTaskLogModel->where("player_id", $playerId)->select();
            $oldId = $oldRow["main_task_id"] ?? 201;
            for ($i = $oldId; $i <= $main_task; $i++) {
                $params = ["platform" => "xiwanwxh5", "player_id" => $playerId, "server_id" => 7,
                    "task_id" => $i, "time" => time()];
                $params["sign"] = Sign::buildMD5sign($params, config("secure." . GAME_NAME . '_key.CLIENT'));
//                var_dump($params);
//                var_dump($this->serverInfo["api_url"] . "/client/update_player");
                $result = Http::post($serverInfo["api_url"] . "/Client/player_task", $params);
                print_r(http_build_query($params) . "====result".$result."\n");
                sleep(0.1);

            }
            $roleTaskLogModel->updateLogData($playerId, ["player_id" => $playerId, "main_task_id" => $main_task]);
        }
    }
}